package com.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.fastjson.JSON;
import com.pojo.LayuiJson;
import com.pojo.Order;
import com.pojo.OrderProduct;
import com.pojo.Product;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class JDBCUtil {
    private static QueryRunner runner =null;
    private static BasicRowProcessor brp = null;
    static {
        InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("druid.properties");
        Properties p = new Properties();
        try {
            p.load(is);
            DataSource ds =DruidDataSourceFactory.createDataSource(p);
            runner = new QueryRunner(ds);
            brp = new BasicRowProcessor(new GenerousBeanProcessor());
        }catch (Exception e){

        }
    }

    //2.封装简化map-->bean

    public static  <T>T mapToBean(Map map, Class<T> c){
        T t=null;
        try {
            t= c.newInstance();
            BeanUtils.populate(t,map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return t;
    }

    //查询对象到集合
    public static <T> List<T> queryList(Class c,String sql,Object...args){
        List<T> list=null;
        try {
            list = runner.query(sql,new BeanListHandler<T>(c,brp),args);
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    //查询单个对象
    public static <T> T queryOne(Class c,String sql , Object...args){
        T t=null;
        try {
            t = runner.query(sql,new BeanHandler<T>(c,brp),args);
        }catch (Exception e){
            e.printStackTrace();
        }
        return t;
    }
    //增删改
    public static int update(String sql,Object...args){
        try {
            return runner.update(sql,args);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    //查询总条数
    public static int queryCount(String sql,Object...args){
        try {
            return runner.query(sql, new ResultSetHandler<Integer>() {
                @Override
                public Integer handle(ResultSet resultSet) throws SQLException {
                    resultSet.next();
                    return resultSet.getInt(1);
                }
            },args);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    //拼接layui所需的数据格式数据
    public static <T> String toLayuiJson(Integer count,List<T> list){
        LayuiJson<T> layuiJson = new LayuiJson<>();
        layuiJson.setCode(0);
        layuiJson.setMsg("");
        layuiJson.setCount(count);
        layuiJson.setData(list);
        return JSON.toJSONString(layuiJson);
    }

    public static boolean deleteImg(String name){
        File file = new File("D:\\upload", name);
        if (file.exists()){
            if (file.delete()){
                return true;
            }
        }
        return false;
    }
}
